package pl.umk.mat.fred.unicore.urc.storagemanagement.dialogs;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.unigrids.x2006.x04.services.sms.PermissionsChangeModeType;
import org.unigrids.x2006.x04.services.sms.PermissionsChangeModeType.Enum;

import pl.umk.mat.fred.unicore.urc.storagemanagement.tree.nodes.BelowStorageNode.NewPermissions;

public class ChangePermissions2Dialog extends Dialog {

	private NewPermissions permissions;
	private NewPermissions newPermissions;
	
	private boolean showRecursivelyOptions;
	private Button recursivelyButton;
	private Boolean recursively;
	private Combo opCombo;
	private Enum mode;
	
	private Button urCheckBoc;
	private Button uwCheckBoc;
	private Button uxCheckBoc;
	
	private Button grCheckBoc;
	private Button gwCheckBoc;
	private Button gxCheckBoc;
	
	private Button orCheckBoc;
	private Button owCheckBoc;
	private Button oxCheckBoc;
	
	public ChangePermissions2Dialog(Shell parentShell, NewPermissions permissions, Boolean showRecursivelyOptions) {
	  super(parentShell);
	  this.permissions = permissions;
	  this.showRecursivelyOptions = showRecursivelyOptions;
  }
	
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite composite = (Composite)super.createDialogArea(parent);
		RowLayout layout = new RowLayout(SWT.VERTICAL);
		layout.marginLeft = 10;
		layout.marginRight = 10;
		composite.setLayout(layout);
		if (permissions == null) {
			Label message = new Label(composite, SWT.NONE);
			message.setText("You are modifying many files simultaneously,");
			Label message2 = new Label(composite, SWT.NONE);
			message2.setText("which have different permissions");
		}
		
		if (showRecursivelyOptions) {
			Group recursiveOptionsGroup = new Group(composite, SWT.NONE);
			recursiveOptionsGroup.setText("Options for folders only");
			GridLayout gridLayout = new GridLayout(2, false);
			recursiveOptionsGroup.setLayout(gridLayout);
			recursivelyButton = new Button(recursiveOptionsGroup, SWT.CHECK);
			recursivelyButton.setText("Change recursively");
			GridData gridData = new GridData();
			gridData.horizontalSpan = 2;
			recursivelyButton.setLayoutData(gridData);
			
			final Label label = new Label(recursiveOptionsGroup, SWT.NONE);
			label.setText("Permissions operation:");
			label.setEnabled(false);
			
			opCombo = new Combo(recursiveOptionsGroup, SWT.READ_ONLY);
			opCombo.add("ADD");
			opCombo.add("REMOVE");
			opCombo.add("SET");
			opCombo.select(2);
			opCombo.setEnabled(false);
			opCombo.addSelectionListener(new SelectionListener() {
				@Override
				public void widgetSelected(SelectionEvent event) {
					if (opCombo.getSelectionIndex() != 2) {
						unselectAll();
					} else {
						restoreDefault();
					}
				}
				
				@Override
				public void widgetDefaultSelected(SelectionEvent arg0) {
				}
			});
			
			recursivelyButton.addListener(SWT.Selection, new Listener() {
				@Override
				public void handleEvent(Event event) {
					if (recursivelyButton.getSelection()) {
						label.setEnabled(true);
						opCombo.setEnabled(true);
						if (opCombo.getSelectionIndex() != 2) {
							unselectAll();
						} else {
							restoreDefault();
						}
					} else {
						label.setEnabled(false);
						opCombo.setEnabled(false);
						restoreDefault();
					}
				}
				
			});
			
		}
		
		Group userGroup = new Group(composite, SWT.NONE);
		userGroup.setText("Owner permissions");
		RowLayout userLayout = new RowLayout(SWT.HORIZONTAL);
		userGroup.setLayout(userLayout);
		urCheckBoc = new Button(userGroup, SWT.CHECK);
		urCheckBoc.setText("Read");
		uwCheckBoc = new Button(userGroup, SWT.CHECK);
		uwCheckBoc.setText("Write");
		uxCheckBoc = new Button(userGroup, SWT.CHECK);
		uxCheckBoc.setText("Execute");
		
		Group groupGroup = new Group(composite, SWT.NONE);
		groupGroup.setText("Group permissions");
		RowLayout groupLayout = new RowLayout(SWT.HORIZONTAL);
		groupGroup.setLayout(groupLayout);
		grCheckBoc = new Button(groupGroup, SWT.CHECK);
		grCheckBoc.setText("Read");
		gwCheckBoc = new Button(groupGroup, SWT.CHECK);
		gwCheckBoc.setText("Write");
		gxCheckBoc = new Button(groupGroup, SWT.CHECK);
		gxCheckBoc.setText("Execute");
		
		Group otherGroup = new Group(composite, SWT.NONE);
		otherGroup.setText("All permissions");
		RowLayout otherLayout = new RowLayout(SWT.HORIZONTAL);
		otherGroup.setLayout(otherLayout);
		orCheckBoc = new Button(otherGroup, SWT.CHECK);
		orCheckBoc.setText("Read");
		owCheckBoc = new Button(otherGroup, SWT.CHECK);
		owCheckBoc.setText("Write");
		oxCheckBoc = new Button(otherGroup, SWT.CHECK);
		oxCheckBoc.setText("Execute");
		
		if (permissions != null) {
			urCheckBoc.setSelection(permissions.getUr());
			uwCheckBoc.setSelection(permissions.getUw());
			uxCheckBoc.setSelection(permissions.getUx());
			
			grCheckBoc.setSelection(permissions.getGr());
			gwCheckBoc.setSelection(permissions.getGw());
			gxCheckBoc.setSelection(permissions.getGx());
			
			orCheckBoc.setSelection(permissions.getOr());
			owCheckBoc.setSelection(permissions.getOw());
			oxCheckBoc.setSelection(permissions.getOx());
		}
		
		return composite;
	}
	
	@Override
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
	}
	
	private void saveInput() {
		mode = PermissionsChangeModeType.SET;
		recursively = Boolean.FALSE;
		if (recursivelyButton != null && recursivelyButton.getSelection()) {
			recursively = Boolean.TRUE;
			switch (opCombo.getSelectionIndex()) {
			case 0: mode = PermissionsChangeModeType.ADD;
				break;
			case 1: mode = PermissionsChangeModeType.SUBTRACT;
				break;
			case 2: mode = PermissionsChangeModeType.SET;
				break;
			default: // DO NOTHING
				break;
			}
		}
		
		newPermissions = new NewPermissions(
				urCheckBoc.getSelection(), uwCheckBoc.getSelection(), uxCheckBoc.getSelection(),
				grCheckBoc.getSelection(), gwCheckBoc.getSelection(), gxCheckBoc.getSelection(),
				orCheckBoc.getSelection(), owCheckBoc.getSelection(), oxCheckBoc.getSelection());
	}
	
	@Override
	protected void okPressed() {
		saveInput();
		super.okPressed();
	}
	
	public boolean areUserPermissionsChanged() {
		return permissions == null ||
				!newPermissions.getUr().equals(permissions.getUr()) ||
				!newPermissions.getUw().equals(permissions.getUw()) ||
				!newPermissions.getUx().equals(permissions.getUx());
	}
	
	public boolean areGroupPermissionsChanged() {
		return permissions == null ||
				!newPermissions.getGr().equals(permissions.getGr()) ||
				!newPermissions.getGw().equals(permissions.getGw()) ||
				!newPermissions.getGx().equals(permissions.getGx());
	}
	
	public boolean areOtherPermissionsChanged() {
		return permissions == null ||
				!newPermissions.getOr().equals(permissions.getOr()) ||
				!newPermissions.getOw().equals(permissions.getOw()) ||
				!newPermissions.getOx().equals(permissions.getOx());
	}
	
	public String getUserPermissions() {
		StringBuilder builder = new StringBuilder();
		
		builder.append(newPermissions.getUr() ? "r" : "-");
		builder.append(newPermissions.getUw() ? "w" : "-");
		builder.append(newPermissions.getUx() ? "x" : "-");
		
		return builder.toString();
	}

	public String getGroupPermissions() {
		StringBuilder builder = new StringBuilder();
		
		builder.append(newPermissions.getGr() ? "r" : "-");
		builder.append(newPermissions.getGw() ? "w" : "-");
		builder.append(newPermissions.getGx() ? "x" : "-");
		
		return builder.toString();
	}
	
	public String getOtherPermissions() {
		StringBuilder builder = new StringBuilder();
		
		builder.append(newPermissions.getOr() ? "r" : "-");
		builder.append(newPermissions.getOw() ? "w" : "-");
		builder.append(newPermissions.getOx() ? "x" : "-");
		
		return builder.toString();
	}
	
	private void restoreDefault() {
		if (permissions != null) {
			urCheckBoc.setSelection(permissions.getUr());
			uwCheckBoc.setSelection(permissions.getUw());
			uxCheckBoc.setSelection(permissions.getUx());
			
			grCheckBoc.setSelection(permissions.getGr());
			gwCheckBoc.setSelection(permissions.getGw());
			gxCheckBoc.setSelection(permissions.getGx());
			
			orCheckBoc.setSelection(permissions.getOr());
			owCheckBoc.setSelection(permissions.getOw());
			oxCheckBoc.setSelection(permissions.getOx());
		} else {
			unselectAll();
		}
  }

	private void unselectAll() {
		urCheckBoc.setSelection(false);
		uwCheckBoc.setSelection(false);
		uxCheckBoc.setSelection(false);
		
		grCheckBoc.setSelection(false);
		gwCheckBoc.setSelection(false);
		gxCheckBoc.setSelection(false);
		
		orCheckBoc.setSelection(false);
		owCheckBoc.setSelection(false);
		oxCheckBoc.setSelection(false);
  }
	
	public Enum getMode() {
		return mode;
	}
	
	public boolean getRecursively() {
		return recursively;
	}

}
